//
//  CCFirtstPageCollectionViewCell.m
//  CwinCar
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCFirtstPageCollectionViewCell.h"
#import "CCFirstPageTableViewCell.h"
@implementation CCFirtstPageCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
        
//        self.number = 1;
      
//        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,mainScreen_Width, mainScree_Height-108) style:UITableViewStylePlain];
//        self.tableView.delegate = self;
//        
//        self.tableView.dataSource = self;
//        [self.contentView addSubview:self.tableView];
//        self.typeArray = @[@18534,@33,@28,@8];
//        [self addHeader];
//        [self addFooter];
    }
    return self;
}


- (void)setDelegate:(id<UITableViewDelegate>)delegate
{
    [_tableView setDelegate:delegate];
}
- (void)setDataSource:(id<UITableViewDataSource>)dataSource
{
    [_tableView setDataSource:dataSource];
}

-(void)addHeader
{
    self.tableArray = nil;
    __weak CCFirtstPageCollectionViewCell *vc = self;
    [_tableView addLegendHeaderWithRefreshingBlock:^{
        vc.number = 1;
        if (self.item == 0) {
            
            [vc creatFirstPage:vc.number];
        }
        else if (self.item == 5) {
            [vc creatData:vc.number];
        }
        else {
             _type = [_typeArray[_item-1]integerValue];
            [vc creatOtherData:vc.number];
        }
        vc.isRefresh = YES;
    }];
    [_tableView.header beginRefreshing];
}
-(void)addFooter
{
     __weak CCFirtstPageCollectionViewCell *vc = self;
    [_tableView addLegendFooterWithRefreshingBlock:^{
        vc.number = vc.number+1;
        if (self.item == 0) {
            
            [vc creatFirstPage:vc.number];
        }
        else if (self.item == 5) {
            [vc creatData:vc.number];
        }
        else{
            _type = [_typeArray[_item-1]integerValue];
            [vc creatOtherData:vc.number];
        }
    }];
    [_tableView.header beginRefreshing];
    
}
//首页
-(void)creatFirstPage:(NSInteger)number
{
    _type = 1;
    
    NSString *url_string = [NSString stringWithFormat:@"http://a.xcar.com.cn/interface/6.0/getNewsList.php?limit=%ld&offset=0&type=%ld&ver=6.1.7",_number * 10,_type];
    [AFNblock getdataWithString:url_string block:^(id block) {
        
        if (self.isRefresh == YES && self.tableArray != nil) {
            [self.tableArray removeAllObjects];
        }
        self.tableArray = [NSMutableArray array];
        self.scrollArr = [NSMutableArray array];
     
        for (NSDictionary *dic in [[block objectForKey:@"focusList"]objectForKey:@"focusImgs"]) {
            CCFirstPageModel *model = [[CCFirstPageModel alloc]init];
            model = [CCFirstPageModel modelWithDictionary:dic];
            [self.scrollArr addObject:model];
        }
        for (NSDictionary *dic in [block objectForKey:@"newsList"]) {
            CCFirstPageModel *model = [[CCFirstPageModel alloc]init];
            model = [CCFirstPageModel modelWithDictionary:dic];
            [self.tableArray addObject:model];
        }
        
        
       
        if (_scrollArr.count > 0) {
            
            [_scroll setData:self.scrollArr];
            
        }
        
        NSString *cachse = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject ;
        NSString *path = [cachse stringByAppendingString:@"/section"];
        NSString *rowpath = [cachse stringByAppendingString:@"/row"];
        [NSKeyedArchiver archiveRootObject:self.scrollArr toFile:path];
        
        BOOL save =   [NSKeyedArchiver archiveRootObject:self.tableArray toFile:rowpath];
        if (save) {
            NSLog(@"成功");
        }else
        {
            NSLog(@"失败");
        }
        
        
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_tableView reloadData];
        
    }];
}


-(void)setItem:(NSInteger)item{
    [_tableArray removeAllObjects];
    [_scrollArr removeAllObjects];
    _tableView.tableHeaderView = nil;
    [_scroll removeFromSuperview];
    [_tableView reloadData];
    [self removeFromSuperview];
    _item = item;
    self.typeArray = @[@18534,@33,@28,@8];
    _tableView = nil;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,-22,mainScreen_Width, mainScree_Height-108) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.contentView addSubview:self.tableView];

    self.tableArray = [NSMutableArray array];
    self.scrollArr = [NSMutableArray array];
    [self sandBox];
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"] isEqualToString:@"yes"] ) {
        
      
        [self addHeader];
        [self addFooter];
        
        
    }
}
//沙盒缓存
-(void)sandBox
{
    
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    _pathStr = [path lastObject];
    
    _pathSection = [_pathStr stringByAppendingString:@"/section"];
    _pathRow = [_pathStr stringByAppendingString:@"/row"];
    
    self.scrollArr = [NSKeyedUnarchiver unarchiveObjectWithFile:_pathSection];
    self.tableArray = [NSKeyedUnarchiver unarchiveObjectWithFile:_pathRow];
    
    if (self.item == 0) {
        _scroll = [[CCFirstScroll alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 150)];
        _tableView.tableHeaderView = _scroll;
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"] isEqualToString:@"yes"] ) {
        
        [self creatFirstPage:1];
        
    }
    else {
         [_scroll setData:self.scrollArr];
    }
        
    }
    
    
    
    
}


//最后一页
-(void)creatData:(NSInteger)number
{
    _type = 2;
    
  
    NSString *url_string = [NSString stringWithFormat:@"http://a.xcar.com.cn/interface/6.0/getNewsList.php?limit=%ld&offset=0&type=%ld&ver=6.1.7",_number * 10,_type];
    [AFNblock getdataWithString:url_string block:^(id block) {
        if (self.isRefresh == YES && self.tableArray != nil) {
            [self.tableArray removeAllObjects];
        }
        self.tableArray = [NSMutableArray array];
        for (NSDictionary *dic in [block objectForKey:@"newsList"]) {
            CCFirstPageModel *model = [[CCFirstPageModel alloc]init];
            model = [CCFirstPageModel modelWithDictionary:dic];
            [self.tableArray addObject:model];
        }
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_tableView reloadData];

        
        
    }];
}
//其他页
-(void)creatOtherData:(NSInteger)number
{
   

    NSString *url_string = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getdingyue.php?limit=%ld&offset=0&type=%ld&ver=6.1.7",_number * 10,_type];
    [AFNblock getdataWithString:url_string block:^(id block) {
        if (self.isRefresh == YES && self.tableArray != nil) {
            [self.tableArray removeAllObjects];
        }
         self.tableArray = [NSMutableArray array];
        for (NSDictionary *dic in [block objectForKey:@"newsList"]) {
            CCFirstPageModel *model = [[CCFirstPageModel alloc]init];
            model = [CCFirstPageModel modelWithDictionary:dic];
            [self.tableArray addObject:model];
        }
      
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_tableView reloadData];

    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.tableArray.count != 0) {
        return self.tableArray.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
        return mainScree_Height / 6.67;
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"tabelViewCell";
    CCFirstPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CCFirstPageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    if (self.tableArray.count > 0) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"] isEqualToString:@"yes"] ) {
            
            
            CCFirstPageModel *model = [self.tableArray objectAtIndex:indexPath.row];
            [cell getCell:model];
            
        }
        else{
            if (self.item == 0) {
                
                CCFirstPageModel *model = [self.tableArray objectAtIndex:indexPath.row];
                [cell getCell:model];

            }
        }
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     CCFirstPageModel *model = [self.tableArray objectAtIndex:indexPath.row];
    CCFormWebViewController *web = [[CCFormWebViewController alloc]init];
    web.webStr = model.webLink;
    
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
//        NSLog(@"$$$$$$$$%@", nextResponder.class);
        if ([nextResponder isKindOfClass:[CCFirstPageViewController class]]) {
            UIViewController *controller = (UIViewController*)nextResponder ;
//            NSLog(@"%@",nextResponder.class);
    
            web.hidesBottomBarWhenPushed = YES;
            [controller.navigationController pushViewController:web animated:YES];
    }
    }
}




@end
